/*
 * Program: Typeit
 * Class that provides keyboard input
 * Beginning Java
 * KIDware (206) 721-2556
 * www.KIDwareSoftware.com
*/  

import java.io.*;

public class Typeit
{

	public static void printPrompt(String prompt)
	{
		// print the prompt
   	System.out.print(prompt + " ");
      System.out.flush();
	}

	public static void inputFlush()
	{
   	int dummy;
   	int bAvail;
   	
   	// flush the data stream
   	try
   	{
   		while((System.in.available()) != 0)
   		{
	  			dummy = System.in.read();
	  		}
   	}
   	catch(java.io.IOException e)
   	{
	 		System.out.println("Input error");
   	}
	}
   
 
	public static String inString(String prompt)
   {
   	// returns input string
      inputFlush();
      printPrompt(prompt);
      return inString();
   }

	public static String inString()
   {
      int aChar;
      String s = "";
      boolean finished = false;

      while(!finished)
      {
			try
         {
		   	aChar = System.in.read();
            if (aChar < 0 || (char)aChar == '\n')
            {
            	finished = true;
            }
            else if ((char)aChar != '\r')
            {
			 		s = s + (char) aChar;   
			 	}
			}
         catch(java.io.IOException e)
         {
				System.out.println("Input error");
		    	finished = true;
         }
      }
      return s;
   }

   
	public static int inInt(String prompt)
   {
   	// returns input int 
   	while(true)
      {
	    	inputFlush();
	     	printPrompt(prompt);
        	try
        	{
		  		return Integer.valueOf(inString().trim()).intValue();
         }
		 	catch(NumberFormatException e)
         { 
		    	System.out.println("Invalid input. Not an integer");
         }
      }
   } 
 

	public static double inDouble(String prompt)
   {
   	// return input double
   	while(true)
      {
      	inputFlush();  
	     	printPrompt(prompt);
         try
         {
		   	return Double.valueOf(inString().trim()).doubleValue();
         }
		  	catch(NumberFormatException e)
         {
		    	System.out.println("Invalid input. Not a floating point number");
         }
      }
   }
}
